/**
 *	主控制器
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */
package com.framework.ACore;

import com.framerock.baselib.Base;
import com.framerock.baselib.CLog;
import com.framerock.baselib.Config;
import com.framerock.baselib.Rock;
import com.rock.xinhujava.RainRock;

import java.lang.reflect.Constructor;


public class Controller extends Base {

    protected void initController() { }

    //需要验证的
    public String checkLogin(){ return "";}

    //需要验证单位
    public String checkCompany(){ return "";}


    /**
     * 设置数据库链接序号
     * */
    public void setDbxu(int dbxu)
    {
        this.dbsort = dbxu;
        this.initBase();
        initController();
    }

    /**
     * 构造控制器里面方法
     * */
    private static Controller getObjectController(String dir, String controller, String folder)
    {
        String classna = "com."+folder+".Controller";
        if(dir.length()>0)classna += "."+dir+"";
        classna += ".Controller"+controller+"";
        try {
            Class<?> clazz = Class.forName(classna);
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            Controller obj = (Controller)constructor.newInstance();
            return obj;
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 返回控制器
     * */
    public static Controller get(int dbxu, String dir, String controller)
    {
        Controller obj = getObjectController(dir, controller, "framework");
        if(obj == null && !Rock.isEmpt(RainRock.MYFOLDER)) {
            obj = getObjectController(dir, controller, RainRock.MYFOLDER);
        }
        if(obj == null)obj = new Controller();
        obj.setDbxu(dbxu);
        return obj;
    }

    /**
    * 执行控制器方法，不可重写
    */
    public String Query(String action)
    {
        String str = "";
        if(action.equals("data")){
            str  = this.data();
        }else if(action.equals("index")){
            str  = this.index();
        }else if(action.equals("submit")){
            str  = this.submit();
        }else if(action.equals("error")){
            str  = this.error();
        }else if(action.equals("check")){
            str  = this.check();
        }else{
            str  = this.runAction(action);
        }
        return str;
    }

    /**
     * 运行控制器方法可重写
     * */
    protected String runAction(String action) { return "action not EXISTS/not REG("+action+")"; }

    /**
     * 定义一些常用方法可重写
     * */
    public String data() { return "empty data"; }
    public String index() { return "empty index"; }
    public String check() { return "empty check"; }
    public String submit() { return "empty submit"; }
    public String error() { return "error action"; }
}
